<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增系统公告')"/>
    <th:block th:include="include :: summernote-css" />
</head>
<body style="background-color: #F4F6FA">
<div class="main-content" style="margin: 0;padding: 0">

    <form id="form-user-add" class="form-horizontal" style="background-color: #F4F6FA">
        <input type="hidden" name="afficheId" value="0"/>
        <input type="hidden" name="flagInt" value="0"/>
        <input type="hidden" name="flagString"/>
        <input type="hidden" name="spareInt" value="0"/>
        <input type="hidden" name="spareString"/>
        <input type="hidden" name="readTimes" value="0"/>
        <input type="hidden" name="objDept"/>
        <input type="hidden" name="objUser"/>
        <input type="hidden" name="attachName"/>
        <input type="hidden" name="attachPath"/>

        <div class="col-sm-12" style="display: flex;justify-content: center">
            <div class="col-sm-10 entering-page">
                <!--系统公告-->
                <div class="col-sm-12" style="margin-top:10px;">
                    <!--标题、类别-->
                    <div class="form-group col-sm-12">
                        <!--工单编号-->
                        <label class="col-sm-1 control-label entering-pd is-required">标题：</label>
                        <div class="col-sm-4">
                            <input name="ntcTitle" placeholder="请输入公告标题" class="form-control" type="text"
                                   required="required">
                        </div>
                        <label class="col-sm-2 control-label entering-pd is-required">类别：</label>
                        <div class="col-sm-2">
<!--                            <select name="sortCode" id="sortCode"-->
<!--                                    th:with="ntcTypes=${@commonServiceImpl.findNtcTypes()}"-->
<!--                                    onchange="dropChgSendTextOnly(this,'sortName');"-->
<!--                                    class="form-control formSelect">-->
<!--                                <th:block th:each="item : ${ntcTypes}">-->
<!--                                    <option th:text="${item['CSText']}" th:value="${item['CSValue']}"-->
<!--                                            th:selected="${item['CSValue'] == '11'}"></option>-->
<!--                                </th:block>-->
<!--                                <input type="hidden" name="sortName" id="sortName" value="普通"/>-->
<!--                            </select>-->
                            <select name="sortCode" id="sortCode"
                                    onchange="dropChgSendTextOnly(this,'sortName');"
                                    class="form-control formSelect">
                                <option value="-1">请选择</option>
                                <option value="11">普通</option>
                                <option value="12">重要</option>
                                <option value="21">紧急</option>
                            </select>
                            <input type="hidden" name="sortName" id="sortName" value="普通"/>

                        </div>
                    </div>
                    <!--通知内容-->
                    <div class="form-group col-sm-12">
                        <label class="col-sm-1 control-label entering-pd">通知内容：</label>
                        <div class="col-sm-11">
                            <div class="ibox float-e-margins">
                                <div class="ibox-content no-padding">
                                    <textarea name="ntcContent" class="summernote">
                                    </textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--来自、发布时间-->
                    <div class="form-group col-sm-12">
                        <!--来自-->
                        <label class="col-sm-1 control-label  entering-pd">来自：</label>
                        <div class="col-sm-4">
                            <input name="fromDept" class="form-control " type="text" th:value="${curUser.getDeptName()}"/>
                            <input type="hidden" name="issueUser" th:value="${curUser.getUserName()}"/>
                        </div>
                        <!--发布时间-->
                        <label class="col-sm-2 control-label  entering-pd">发布时间：</label>
                        <div class="col-sm-4">
                            <input name="issueTime" id="issueTime" th:value="${#dates.format(now,'yyyy-MM-dd HH:mm:ss')}" class="form-control" type="text"/>
                        </div>
                        <div class="input-group-append " style="position: absolute;top: 10px;right: 120px;">
                            <span class="glyphicon glyphicon-calendar "></span>
                        </div>
                    </div>
                    <!--责任部门、联系电话-->
                    <div class="form-group col-sm-12" id="tellTo">
                        <label class="col-sm-1 control-label entering-pd">通知对象：</label>
                        <div class="col-sm-11">
                            <div class="col-sm-4" id="allIn">
                                <label class="check-box">
                                    <span class="spLBlue entering-pd">全选</span>
                                    <input type="checkbox" name="checkAll" id="chkAll" />
                                </label>
                            </div>
                            <div th:if="${@sysRoleServiceImpl.selectRoleAll().size() == 0}">
                                <div class="col-sm-4" style="margin-top:-3px;">
                                    <input type="text" id="deptSearch" name="search" class="form-control" placeholder="请输入需要查找的内容">
                                </div>
                                <div class="col-sm-1" style="margin-left:-25px;">
                                    <button type="button" class="btn btn-sm btn-info" id="dept-search">查找</button>
                                </div>
                            </div>
                            <div class="check-box-table-cell col-sm-12 find-list">
                                <div th:if="${@sysRoleServiceImpl.selectRoleAll().size() != 0}">
                                <!--                                <table cellSpacing="1" cellPadding="1" width="100%" border="0" th:with="roles=${@SRoleInfoServiceImpl.roleInfoAll()}">-->
                                    <table cellSpacing="1" cellPadding="1" width="100%" border="0"  th:with="roles=${@sysRoleServiceImpl.selectRoleAll()}">
                                        <th:block th:each="entry,stat: ${roles}">
                                            <span th:if="${stat.index % 4 == 0 || stat.index == 0}">
                                                <tr>
                                            </span>
                                            <td>
                                                <input name="roleIds"
                                                       th:value="${entry.roleId}"
                                                       th:text="${entry.roleName}"
                                                       style="margin-right:3px" type="checkbox" class="checkbox-inline"/>
                                            </td>
                                        </th:block>
                                    </table>
                                    <input type="hidden" name="tell" value="1"/>
                                </div>
                                <div th:if="${@sysRoleServiceImpl.selectRoleAll().size() == 0}" id="search-append">
                                    <div>
                                        <ul class="deptTransfer-ul" th:with="allDepts=${@commonServiceImpl.allDepartment('__${curUser.getDeptId()}__')}">
                                            <li class="deptTransfer-li" th:each="entry:${allDepts}">
                                                <label>
                                                    <input name="roleIds"
                                                           th:value="${entry.noUnit}"
                                                           th:text="${entry.deptShort}"
                                                           style="margin-right:3px" type="checkbox" class="checkbox-inline"/>
                                                </label>
                                            </li>
                                        </ul>
                                    </div>
                                    <input type="hidden" name="tell" value="2"/>
                                </div>
                                <input type="hidden" id="objRole" name="objRole"/>
                            </div>
                        </div>

                    </div>

                    <!--发布、置顶-->
                    <div class="form-group col-sm-12">
                        <label class="col-sm-2 control-label entering-pd">发布：</label>
                        <div class="col-sm-4">
                            <label class="check-box">
                                <input type="radio" name="issueFlag" value="1" checked/>是
                            </label>
                            <label class="check-box">
                                <input type="radio" name="issueFlag" value="0" />否
                            </label>
                        </div>
                        <label class="col-xs-2 control-label entering-pd">置顶：</label>
                        <div class="col-xs-4">
                            <label class="check-box">
                                <input type="radio" name="topFlag" value="1" />是
                            </label>
                            <label class="check-box">
                                <input type="radio" name="topFlag" value="0" checked/>否
                            </label>
                        </div>
                    </div>
                    <!-- 备注-->
                    <div class="form-group col-sm-12 pb20">
                        <label class="col-xs-2 control-label entering-pd">备注：</label>
                        <div class="col-xs-4">
                            <input name="remark" placeholder="请输入" class="form-control " type="text"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-2">
                <!--按钮-->
                <div class="col-sm-12 entering-bt" style="margin-top: 10px;position:fixed;">
                    <button type="button" class="btn btn-sm btn-info" onclick="doSubmit()">保存
                    </button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <button type="button" class="btn btn-sm btn-warning" onclick="closeItem()">关闭
                    </button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </div>
            </div>
        </div>
    </form>
</div>

<script th:src="@{/ajax/libs/layui/laydate.js}"></script>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<script>
    var prefix = "/system/mInterior/cfNotice";

    $(document).ready(function () {
        var roleId=$("#objRole").val().toString();
        if(roleId=="-1"){
            document.getElementById("chkAll").checked=true;
        }

        $('.summernote').summernote({
            lang: 'zh-CN',
            tabsize: 2,
            height: 150,//高度
            followingToolbar: false,
            callbacks: {
                onKeydown: function (e) {
                    e.stopPropagation();
                },
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
        });
    });
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function(result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function(error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    function doSubmit(){
        if(document.getElementById("chkAll").checked){
            $("#objRole").val("-1");
        }
        else{
            var ids="";
            var arrSons = document.getElementsByName("roleIds");
            for (var i=0; i<arrSons.length; i++) {
                if (arrSons[i].checked){
                    ids += arrSons[i].value + ",";
                }
            }
            $("#objRole").val(ids.substring(0, ids.length-1));
        }
        if($.validate.form()) {
            var url = prefix + '/afficheAdd';
            var data = $('#form-user-add').serializeArray();
            if($("input[name='tell']").val() == 2){
                let chkVal = $("#objRole").val();
                $.each(data,function (index) {
                    console.log(data[index])
                    if(data[index].name == 'objDept'){
                        data[index].value = chkVal;
                    }
                    if(data[index].name =='objRole'){
                        data[index].value = '';
                    }
                })
            }
            $.operate.saveTab(url,data);
        }
    }

    //回车快捷键
    window.onkeyup=function (){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 Enter
            console.log(e)
        }
    }

    //搜索
    $('#tellTo').on('click','#dept-search',function (){
        $('#allIn').hide();
        var deptList = $('div').find('li');
        var deptSearch = $('#deptSearch').val();
        $.each(deptList,function (index,value){
            var val = $(value).text();
            var item = $(deptList[index]);//下标
            if(deptSearch === ''){
                $('#allIn').show();
                item.show();
            }else if(val.indexOf(deptSearch) === -1){
                item.hide();
            }else{
                item.show();
            }
        })
        var isShow = true;
        $.each(deptList,function(index,value){
            var item = $(deptList[index]);
            if (item.is(':visible')){
                isShow = false;
            }
        })
        if (isShow) {
            if($(deptList[0]).text() !== '未查询到匹配数据') {
                var oldHtml = $('.find-list').find('ul').html();
                $('.find-list').find('ul').html('<li class="deptTransfer-li">未查询到匹配数据</li>' + oldHtml);
            }else {
                $(deptList[0]).show()
            }
        }else if($(deptList[0]).text() === '未查询到匹配数据'){
            $(deptList[0]).hide()
        }
    })

    $('#chkAll').on('ifClicked',function(obj) {
        var oChkAll = document.getElementById("chkAll");
        var arChkSons = document.getElementsByName("roleIds");
        var tempState= !oChkAll.checked;
        for(var i=0;i<arChkSons.length;i++) {
            if(arChkSons[i].checked != tempState) {
                arChkSons[i].checked = tempState;
            }
        }
    })

    $('#form-user-add').validate({
        rules: {
            sortCode: {
                required: true,
                min: 1
            }
        },
        messages: {
            sortCode: {
                min: ''
            }
        }
    });

    laydate.render({
        elem: '#issueTime',
        type: 'datetime',
        trigger: 'click'
    });
</script>
</body>
</html>

